home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Misc / Crossword / Source / Puzzle.m < prev    next >
Text File  |  1992-10-11  |  3KB  |  130 lines

  1. /*
  2.  
  3. File Puzzle.m
  4.  
  5. The puzzle object links a crossword matrix to a dictionary and a search.  The puzzle has various inspectable parameters that the user can set to control the type of search.
  6.  
  7. */
  8.  
  9. #import <appkit/appkit.h>
  10. #import <stdlib.h>
  11.  
  12. #import "Puzzle.h"
  13. #import "Inspector.h"
  14. #import "StopWatch.h"
  15. #import "Crossword.h"
  16. #import "CrosswordSquare.h"
  17. #import "FunctionCache.h"
  18.  
  19. #import "PlainState.h"
  20. #import "BackjumpState.h"
  21. #import "LeapfrogState.h"
  22. #import "DepthFirstSearch.h"
  23. #import "BroadeningSearch.h"
  24.  
  25.  
  26. /* ————————————————————————————————————————————————————————————————————————————  */
  27.  
  28.  
  29. #define PLAIN        0
  30. #define BACKJUMP    1
  31. #define LEAPFROG    2
  32.  
  33.  
  34. /* ————————————————————————————————————————————————————————————————————————————  */
  35.  
  36.  
  37. @implementation Puzzle
  38.  
  39. - getCrossword            {    return crossword;        }
  40. - getInspector            {    return inspector;        }
  41. - getDictionary            {    return dictionary;        }
  42. - getState                {    return state;            }
  43. - (BOOL) canContinue    {    return canContinue;        }
  44.  
  45.  
  46. /* ————————————————————————————————————————————————————————————————————————————  */
  47.  
  48.  
  49. - free
  50. {
  51.     [dictionary  free];
  52.     [search  free];
  53.     [state  free];
  54.     
  55.     return [super  free];
  56. }
  57.  
  58.  
  59. /* ————————————————————————————————————————————————————————————————————————————  */
  60.  
  61.  
  62. /*
  63.  
  64. Here are the routines that put together the search.  The biggest step is extracting the squares and words from the puzzle.  Then the search is initialized and executed.
  65.  
  66. */
  67.  
  68. - newSearch: sender
  69. {
  70.     id                stateType, searchType;
  71.     StopWatch        * watch;
  72.     
  73.     [search  free];
  74.     [state  free];
  75.     [crossword  clear: [inspector  getColor: 0]];
  76.     
  77.     switch ([inspector  getStateType])
  78.     {
  79.         case PLAIN:        stateType = [PlainState  class];        break;
  80.         case BACKJUMP:    stateType = [BackjumpState  class];        break;
  81.         case LEAPFROG:    stateType = [LeapfrogState  class];        break;
  82.         default:        stateType = nil;                        break;
  83.     }
  84.     
  85.     switch ([inspector  getOption: BROADENING])
  86.     {
  87.         case NO:    searchType = [DepthFirstSearch  class];        break;
  88.         case YES:    searchType = [BroadeningSearch  class];        break;
  89.         default:    searchType = nil;                            break;
  90.     }
  91.     
  92.     [search = [searchType  alloc]  init];
  93.     [state = [stateType  alloc]  initPuzzle: self];
  94.     [[watch = [inspector  getWatch]  reset]  start];
  95.     canContinue = [search  startSearch: [state  getSquares]];
  96.     
  97.     [watch  stop];
  98.     [inspector  showTime];
  99.     
  100.     return self;
  101. }
  102.  
  103.  
  104. - continue: sender
  105. {
  106.     StopWatch        * watch;
  107.     
  108.     [watch = [inspector  getWatch]  start];
  109.     canContinue = [search  continue];
  110.     [watch  stop];
  111.     [inspector  showTime];
  112.     
  113.     return self;
  114. }
  115.  
  116.  
  117. - step: sender
  118. {
  119.     StopWatch        * watch;
  120.     
  121.     [watch = [inspector  getWatch]  start];
  122.     canContinue = [search  step];
  123.     [watch  stop];
  124.     [inspector  showTime];
  125.     
  126.     return self;
  127. }
  128.  
  129.  
  130. @end